<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}菜单`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="父级菜单" prop="parentId">
        <el-tree-select
          :props="{ value: 'id', label: data => data.meta?.title }"
          v-model="drawerProps.row!.parentId"
          :data="menuTree"
          check-strictly
          :render-after-expand="false"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="菜单名称" prop="meta.title">
        <el-input v-model.trim="drawerProps.row!.meta!.title" placeholder="请填写菜单名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="菜单图标" prop="meta.icon">
        <SelectIcon v-model:icon-value="drawerProps.row!.meta!.icon" />
      </el-form-item>
      <el-form-item label="菜单name" prop="name">
        <el-input v-model.trim="drawerProps.row!.name" placeholder="请填写菜单name" clearable></el-input>
      </el-form-item>
      <el-form-item label="菜单路径" prop="path">
        <el-input v-model.trim="drawerProps.row!.path" placeholder="请填写菜单路径" clearable></el-input>
      </el-form-item>
      <el-form-item label="组件路径" prop="component">
        <el-input v-model.trim="drawerProps.row!.component" placeholder="请填写组件路径" clearable></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="drawerProps.row!.sort" placeholder="请填写排序" clearable></el-input>
      </el-form-item>
      <el-form-item label="是否隐藏" prop="meta.isHide">
        <el-radio-group v-model="drawerProps.row!.meta!.isHide">
          <el-radio :value="true" size="large">是</el-radio>
          <el-radio :value="false" size="large">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否全屏" prop="meta.isFull">
        <el-radio-group v-model="drawerProps.row!.meta!.isFull">
          <el-radio :value="true" size="large">是</el-radio>
          <el-radio :value="false" size="large">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false"> 取消 </el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="MenuDrawer">
import SelectIcon from "@/components/SelectIcon/index.vue";
import { ref, reactive } from "vue";
import { getAuthMenuListApi } from "@/api/modules/login";
import { ElMessage, FormInstance } from "element-plus";

const rules = reactive({
  title: [{ required: true, message: "请填写菜单名称" }],
  icon: [{ required: true, message: "请填写菜单图标" }],
  name: [{ required: true, message: "请填写菜单name" }],
  path: [{ required: true, message: "请填写菜单路径" }],
  component: [{ required: true, message: "请填写组件路径" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Menu.MenuOptions>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const menuTree = ref<any>([]);
const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  if ("编辑" !== params.title) {
    params.row.meta = { title: "", icon: "" };
    params.row.meta.isFull = false;
  }
  const res = await getAuthMenuListApi();
  let data = [{ id: 0, meta: { title: "顶级菜单" }, children: res.data }];
  menuTree.value = data;
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}菜单成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
